package com.github.postsanf.yinian.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.NewsFragmentAdapter;
import com.github.postsanf.yinian.bean.CoverThemeTitle;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.entity.YNCoverThemeTitleResponse;
import com.github.postsanf.yinian.fragment.VoiceCardCoverThemeFragment;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class VoiceCardCoverMore extends BaseActivity implements OnClickListener {
    private TabLayout mTabLayout;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_cover_more);
        registerBroadcastReceiver();
        initView();
        initdata();
    }

    private void registerBroadcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTIONs.actionUpdateCoverImg);
        this.registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTIONs.actionUpdateCoverImg:
                    finish();
                    break;
            }
        }
    };

    private void initView() {
        mTabLayout = (TabLayout) findViewById(R.id.tl_covermore_tabs);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mViewPager = (ViewPager) findViewById(R.id.vp_covermore_viewpager);
        findViewById(R.id.covermore_back).setOnClickListener(this);
        findViewById(R.id.covermore_sure).setOnClickListener(this);
    }

    private void initdata() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put("type", "3");
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.GetCoverTheme))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNCoverThemeTitleResponse resAlertBg = gson.fromJson(s, YNCoverThemeTitleResponse.class);
                        if (ReqUtils.isSuccess(resAlertBg.getCode())) {
                            List<CoverThemeTitle> titlelist = resAlertBg.getData();
                            List<Fragment> fragments = new ArrayList<>();
                            List<String> titles = new ArrayList<>();
                            if (titlelist.size() > 0) {
                                for (int i = 0; i < titlelist.size(); i++) {
                                    String tt = titlelist.get(i).getOfficialCoverTheme();
                                    titles.add(tt);
                                    mTabLayout.addTab(mTabLayout.newTab().setText(tt));
                                    fragments.add(VoiceCardCoverThemeFragment.newInstance(tt));
                                }
                                NewsFragmentAdapter adapter = new NewsFragmentAdapter(getSupportFragmentManager(), fragments, titles);
                                mViewPager.setAdapter(adapter);
                                mTabLayout.setupWithViewPager(mViewPager);
                                mTabLayout.setTabsFromPagerAdapter(adapter);
                            }
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.unregisterReceiver(mBroadcastReceiver);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.covermore_back:
                finish();
                break;
        }
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }

}
